﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Converters;
using System.Collections;
using Newtonsoft.Json;
using System.Runtime.InteropServices;

namespace OnTheWay.Domain
{
    public class ServiceTransfer
    {
        public string ReturnValue { get; set; }
        public string Exception { get; set; }
        public IList<String> ListValue { get; set; }

        private ServiceTransfer() { }

        internal static IsoDateTimeConverter mDateTimeConverter = null;

        static ServiceTransfer()
        {
            mDateTimeConverter = new IsoDateTimeConverter();
            mDateTimeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
        }

        public string DecodeException
        {
            get
            {
                // TODO Auto-generated method stub
                return Encoding.UTF8.GetString(Convert.FromBase64String(Exception));
            }
        }

        public String DecodeReturnValue
        {
            get
            {
                // TODO Auto-generated method stub
                return Encoding.UTF8.GetString(Convert.FromBase64String(ReturnValue));
            }
        }

        public IList<String> DecodeListValue
        {
            get
            {
                // TODO Auto-generated method stub
                List<String> list = new List<String>();
                foreach (String str in ListValue)
                {
                    list.Add(Encoding.UTF8.GetString(Convert.FromBase64String(str)));
                }
                return list;
            }
        }

        public int ReturnInt
        {
            get
            {
                // TODO Auto-generated method stub
                return JsonConvert.DeserializeObject<int>(DecodeReturnValue);//mGson.fromJson(getReturnValue(), int.class);
            }
        }

        public long ReturnLong
        {
            get
            {
                // TODO Auto-generated method stub
                return JsonConvert.DeserializeObject<long>(DecodeReturnValue);//mGson.fromJson(getReturnValue(), long.class);
            }
        }

        public bool ReturnBoolean
        {
            get
            {
                // TODO Auto-generated method stub
                return JsonConvert.DeserializeObject<bool>(DecodeReturnValue);//mGson.fromJson(getReturnValue(), boolean.class);
            }
        }

        public T GetReturn<T>()
        {
            // TODO Auto-generated method stub
            return JsonConvert.DeserializeObject<T>(DecodeReturnValue, mDateTimeConverter);//mGson.fromJson(getReturnValue(), clazz);
        }

        internal IList<T> GetReturnList<T>()
        {
            IList<T> list = new List<T>();
            foreach (var item in DecodeListValue)
            {
                list.Add(JsonConvert.DeserializeObject<T>(item, mDateTimeConverter));
            }
            return list;
        }

        public static ServiceTransfer FromListValue(IEnumerable list)
        {
            string temp;
            ServiceTransfer holder = new ServiceTransfer();
            holder.ListValue = new List<String>();
            foreach (var item in list)
            {
                temp = JsonConvert.SerializeObject(item, Formatting.Indented, mDateTimeConverter);
                temp = Convert.ToBase64String(Encoding.UTF8.GetBytes(temp));
                holder.ListValue.Add(temp);
            }
            return holder;
        }
        public static ServiceTransfer FromReturnValue(Object obj)
        {
            ServiceTransfer holder = new ServiceTransfer();
            holder.ReturnValue = JsonConvert.SerializeObject(obj, Formatting.Indented, mDateTimeConverter);
            holder.ReturnValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(holder.ReturnValue));
            return holder;
        }
        public static ServiceTransfer FromException(_Exception ex)
        {
            ServiceTransfer holder = new ServiceTransfer();
            holder.Exception = Convert.ToBase64String(Encoding.UTF8.GetBytes(ex.Message));
            return holder;
        }

    }
}
